//
// Created by hequn on 2021/9/16.5:09 下午
//

#include <stdlib.h>
#include <stdio.h>

#define MAXSIZE  1024
typedef char datatype;
typedef struct {
    datatype data[MAXSIZE];
    int top;
} SeqStack;

//顺序栈置空栈：首先建立栈空间，然后初始化栈顶指针。
SeqStack *Init_SeqStack() {
    SeqStack *s;
    s = (SeqStack *) malloc(sizeof(SeqStack));
    s->top = -1;
    return s;
}

//顺序栈判空栈
int Empty_SeqStack(SeqStack *s) {
    if (s->top == -1) return 1;
    else return 0;
}

//顺序栈入栈
int Push_SeqStack(SeqStack *s, datatype x) {
    if (s->top == MAXSIZE - 1) return 0; //栈满不能入栈
    else {
        s->top++;
        s->data[s->top] = x;
        return 1;
    }
}

//顺序栈出栈
int Pop_SeqStack(SeqStack *s, datatype *x) {
    if (Empty_SeqStack(s)) return 0; //栈空不能出栈
    else {
        *x = s->data[s->top];
        s->top--;
        return 1;        //栈顶元素存入*x，返回
    }
}

int Expression(SeqStack *top) {
    char x;
    char ch;
    printf("输入待匹配的表达式字符串: 回车键结束\n");
    scanf("%c",&ch);
    
   while (ch != '\n') {
       if (ch == '(') {
           Push_SeqStack(top, ch);
       } else if (ch == ')') {
           if (top->top == -1) {
               return 0;
           } else {
               Pop_SeqStack(top, &x);
           }
       }
       ch = getchar();
   }
   if (top->top ==-1){
       return 1;
   } else{
       return 0;
   }
    
}

int main() {

    SeqStack *top;
    top = Init_SeqStack();
    int flag;
    flag = Expression(top);
    printf("%d",flag);
    if (flag){
        printf("匹配成功\n");
    } else{
        printf("匹配失败\n");
    }
    return 0;
}
